package ro.ubbcluj.cs.bibliospringee.backend.service.beans;

import ro.ubbcluj.cs.bibliospringee.backend.model.Author;
import ro.ubbcluj.cs.bibliospringee.backend.repository.AuthorRepository;
import ro.ubbcluj.cs.bibliospringee.backend.repository.exception.BiblioSpringRepositoryException;
import ro.ubbcluj.cs.bibliospringee.backend.service.AuthorService;
import ro.ubbcluj.cs.bibliospringee.backend.service.exception.BiblioSpringServiceException;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;

@Stateless(name = "AuthorService", mappedName = "ejb/AuthorService")
@DependsOn({"AuthorRepository", "Author"})
public class AuthorServiceBean implements AuthorService
{

    @EJB
    private AuthorRepository authorRepository;

    @Override
    public List<Author> getAll() throws BiblioSpringServiceException
    {
        try
        {
            return authorRepository.getAll();
        }
        catch (final BiblioSpringRepositoryException bbre)
        {
            throw new BiblioSpringServiceException("Author repository - getAll()", bbre);
        }
    }

    @Override
    public Author getById(final Long id) throws BiblioSpringServiceException
    {
        try
        {
            return authorRepository.getById(id);
        }
        catch (final BiblioSpringRepositoryException e)
        {
            throw new BiblioSpringServiceException("AuthorRepositoryBean getById exception", e);
        }
    }

    @Override
    public Author insertAuthor(final Author author) throws BiblioSpringServiceException
    {
        try
        {
            System.out.println("insert author");
            return authorRepository.save(author);
        }
        catch (final BiblioSpringRepositoryException e)
        {
            throw new BiblioSpringServiceException("AuthorRepositoryBean insertAuthor exception", e);
        }
    }

    @Override
    public void deleteAuthor(Author author) throws BiblioSpringServiceException
    {
        try
        {
            System.out.println("delete author");
            authorRepository.delete(author);
        }
        catch (final BiblioSpringRepositoryException e)
        {
            throw new BiblioSpringServiceException("AuthorRepositoryBean deleteAuthor exception", e);
        }
    }
}
